#!/usr/bin/env bash
set -e

exec 2>&1

APP=cannon

# defaults
USER=${USER:-www-data}
CONFIG=${CONFIG:-/etc/$APP/.env}
HOME=${APP_HOME:-/opt/$APP}
BIN=$HOME/bin/$APP

if [ ! -f $CONFIG ]; then exec chpst -u $USER get_config; fi
echo $CONFIG
source $CONFIG

export LOG_LEVEL=${CANNON_LOG_LEVEL:-Info}
export LOG_BUFFER=${CANNON_LOG_BUFFER:-4096}
export LOG_NETSTR=${CANNON_LOG_NETSTR:-True}
export GHCRTS='-I5'

cd $HOME

ulimit -n 131072

exec chpst -u $USER \
    $BIN \
    -p ${CANNON_PORT?'unset'} \
    --host ${CANNON_HOST?'unset'} \
    --external-host ${CANNON_EXTERNAL_HOST:-127.0.0.1} \
    --gundeck-host=${CANNON_GUNDECK_HOST?'unset'} \
    --gundeck-port=${CANNON_GUNDECK_PORT?'unset'}
